import React from 'react';
import PropTypes from 'prop-types';
import {Card, Button, Row, Col} from 'antd';
import {DATE_FORMAT} from '../constants';
import DescriptionList from 'ant-design-pro/lib/DescriptionList';
import routes from './routes';
import {abandonOrder} from './api';
import {injectIntl} from './intl';
import {salesOPSubmitLimitType, baseDataStatus} from '../Enum';
import {conventEnumValueToString, formatDateTime} from '../utils';
import {hasPermissions} from './utils';
import {PERMISSION} from './constants';
import WrappedPopconfirm from '../common/WrappedPopconfirm';
import styles from './style.css';
const {Description} = DescriptionList;
export class DetailPanel extends React.PureComponent {
    componentDidMount() {
        this.props.init();
    }
    handleUpdateClick = () => {
        this.props.history.push(routes.update.format(this.props.id));
    }
    onClickAbandonBtn = () => {
        this.setState({loading: true});
        abandonOrder(this.props.detail.id, this.props.detail.rowVersion)
            .then(res => {
                this.setState({loading: false});
                if(res.ok)
                    this.props.successReturn();
            });
    }
    
    render() {
        const {formatMessage} = this.props.intl;
        const {options} = this.props.detail;
        const operations = [];
        if(hasPermissions(this.props.permissions, PERMISSION.update) && hasPermissions(options, PERMISSION.update))
            operations.push(<Button key="update" onClick={this.handleUpdateClick} size="small">{
                formatMessage({
                    id: 'detailPage.btn.update',
                    defaultMessage: '编辑'
                })
            }</Button>);
        if(hasPermissions(this.props.permissions, PERMISSION.abandon) && hasPermissions(options, PERMISSION.abandon))
            operations.push(<WrappedPopconfirm
                key="abandon"
                id={this.props.id}
                placement="topLeft"
                onConfirm={this.onClickAbandonBtn}
                title={formatMessage({
                    id: 'detailPanel.validate.abandon',
                    defaultMessage: '是否确认作废？'
                })}>
                <Button key="abandon" size="small">{
                    formatMessage({
                        id: 'detailPanel.btn.abandon',
                        defaultMessage: '作废'
                    })
                }</Button>
            </WrappedPopconfirm>);
        return (
            <div className="detail">
                <Card loading={this.props.loading}>
                    <DescriptionList size="large" title={<Row><Col span={12}>{formatMessage({
                        id: 'detailPanel.text.title',
                        defaultMessage: '基本信息'
                    })}</Col>
                    <Col className={`col-align-right ${styles.detail}`} span={12}>{operations}</Col>
                    </Row>}>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.brandCode',
                            defaultMessage: '品牌编号'
                        })}>{this.props.detail.brandCode}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.brandName',
                            defaultMessage: '品牌名称'
                        })}>{this.props.detail.brandName}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.dealerCode',
                            defaultMessage: '总代编号'
                        })}>{this.props.detail.dealerCode}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.dealerName',
                            defaultMessage: '总代名称'
                        })}>{this.props.detail.dealerName}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.type',
                            defaultMessage: '提报时效类型'
                        })}>{conventEnumValueToString(salesOPSubmitLimitType, this.props.detail.type)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.submitBeginDay',
                            defaultMessage: '提报开始日'
                        })}>{this.props.detail.submitBeginDay}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.submitEndDay',
                            defaultMessage: '提报结束日'
                        })}>{this.props.detail.submitEndDay}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.submitNoticeBeginDay',
                            defaultMessage: '提报提醒开始日'
                        })}>{this.props.detail.submitNoticeBeginDay}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.submitNoticeEndDay',
                            defaultMessage: '提报提醒结束日'
                        })}>{this.props.detail.submitNoticeEndDay}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.effectiveDate',
                            defaultMessage: '生效日期'
                        })}>{formatDateTime(this.props.detail.effectiveDate, DATE_FORMAT)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.expirationDate',
                            defaultMessage: '失效日期'
                        })}>{formatDateTime(this.props.detail.expirationDate, DATE_FORMAT)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.abandonerName',
                            defaultMessage: '作废人姓名'
                        })}>{this.props.detail.abandonerName}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.abandonTime',
                            defaultMessage: '作废时间'
                        })}>{formatDateTime(this.props.detail.abandonTime, DATE_FORMAT)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.status',
                            defaultMessage: '状态'
                        })}>{conventEnumValueToString(baseDataStatus, this.props.detail.status)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.remark',
                            defaultMessage: '备注'
                        })}>{this.props.detail.remark}</Description>
                    </DescriptionList>
                </Card>
                <div className="page-toolbar">
                    <Button
                        type="primary"
                        disabled={this.props.loading}
                        onClick={this.props.onCancel}>{formatMessage({
                            id: 'detailPage.text.return',
                            defaultMessage: '返回'
                        })}</Button>
                </div>
            </div>
        );
    }
}

DetailPanel.propTypes = {
    detail: PropTypes.object.isRequired,
    id: PropTypes.string.isRequired,
    init: PropTypes.func.isRequired,
    loading: PropTypes.bool.isRequired,
    permissions: PropTypes.array.isRequired,
    onCancel: PropTypes.func.isRequired,
    history: PropTypes.object,
    intl: PropTypes.object,
    successReturn: PropTypes.func,
};

import {connect} from 'react-redux';
import {
    getDetail,
    tableSearch
} from './actions';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
const getPermissions = selectorFactory(['page', 'domainData', 'permission', 'data']);
const getDetailData = selectorFactory(['page', 'domainData', 'detail', 'data']);


const mapStateToProps = (state, props) => ({
    id: props.id,
    loading: state.getIn(['page', 'domainData', 'detail', 'isFetching']),
    detail: getDetailData(state),
    permissions: getPermissions(state),
});
const mapDispatchToProps = (dispatch, props) => ({
    init: () => dispatch(getDetail(props.id)),
    successReturn: () => {
        dispatch(tableSearch());
        props.history.push(routes.query.url());
    },
    onCancel: () => {
        props.history.push(routes.query.url());
    },
});
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(DetailPanel));
